home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / New System Software Extensions / QuickDraw™ GX v1.0ß2 / Interfaces & Libraries / graphics libraries / graphicsBug library.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-19  |  8.1 KB  |  210 lines  |  [TEXT/MPS ]

  1. /* graphics libraries:
  2.     
  3.     graphicsBug Library.c 
  4.  
  5.     ©1989 - 1993 Apple Computer, Inc.
  6.     All rights reserved.
  7.     
  8. */
  9. /* this is so that we get the trap version of Gestalt */
  10. #define SystemSevenOrLater 1
  11.     
  12. #ifdef THINK_C
  13.     #include <MacHeaders>
  14. #endif
  15. #include <GestaltEqu.h>
  16. #include <Processes.h>
  17. #include <string.h>
  18. #include "math types.h"
  19. #include "memory types.h"
  20. #include "graphics debugging.h"
  21. #include "graphicsBug library.h"
  22.  
  23.  
  24. // *** eventually, allow this to work both locally and over the network?
  25. // *** for remote debugging, lauch gBug on remote machine, and then use special menu item
  26. // to bring up PPCToolbox browser. Select one or more machines running graphics init to receive 
  27. // errors from, or to set break points, etc.
  28.  
  29. boolean SendMessageToGraphicsBug(long command, const char *string, unsigned long message, boolean waitForCompletion)
  30. {
  31.     ProcessSerialNumber     graphicsBugProcess;
  32.     OSErr               err;
  33.     struct graphicsBugParameters *blockPointer = nil;
  34.     EventRecord         theEvent;
  35.     
  36.     {   LaunchParamBlockRec     launch;
  37.         DTPBRec             bugAp;
  38.         FSSpec              fileSpecification;
  39.         
  40.         bugAp.ioNamePtr = nil;
  41.         bugAp.ioVRefNum = 0;
  42.         if (err = PBDTGetPath(&bugAp))
  43.             return false; /* error */
  44.         bugAp.ioNamePtr = fileSpecification.name;
  45.         bugAp.ioCompletion = nil;
  46.         bugAp.ioIndex = 0;
  47.         bugAp.ioFileCreator = creatorType;
  48.         if (err = PBDTGetAPPL(&bugAp, false))
  49.             return false; /* error */
  50.         /* is the file already launched ? */
  51.         {   ProcessInfoRec processInfo;
  52.             FSSpec processSpec;
  53.             
  54.             processInfo.processName = nil;
  55.             processInfo.processAppSpec = &processSpec;
  56.             processInfo.processInfoLength = sizeof(processInfo);
  57.             graphicsBugProcess.highLongOfPSN = 0;
  58.             graphicsBugProcess.lowLongOfPSN = kNoProcess;
  59.             while (GetNextProcess(&graphicsBugProcess) == noErr) {
  60.                 if (GetProcessInformation(&graphicsBugProcess, &processInfo) == noErr &&
  61.                     processInfo.processType == 'APPL' &&
  62.                     processInfo.processSignature == creatorType &&
  63.                     processInfo.processAppSpec->parID == bugAp.ioAPPLParID)
  64.                 {
  65.                     goto doEvent;
  66.                 }
  67.             }
  68.         }
  69.         launch.launchBlockID = extendedBlock;
  70.         launch.launchEPBLength = extendedBlockLen;
  71.         launch.launchFileFlags = 0;
  72.         launch.launchControlFlags = launchDontSwitch | launchContinue | launchNoFileFlags;
  73.         fileSpecification.vRefNum = 0;
  74.         fileSpecification.parID = bugAp.ioAPPLParID;
  75.         launch.launchAppSpec = &fileSpecification;
  76.         launch.launchAppParameters = nil;
  77.         if (err = LaunchApplication(&launch))
  78.             return false; /* error */
  79.         if (launch.launchMinimumSize)  /* ap not already open */
  80.         {   register short counter = 4;
  81.         
  82.             do
  83.                 WaitNextEvent(0, &theEvent, -1, nil);
  84.             while (--counter);
  85.         }
  86.         graphicsBugProcess = launch.launchProcessSN;
  87.     }
  88. doEvent:
  89. #ifdef __APPLEEVENTS__
  90.     {   AEAddressDesc   targetAddress;
  91.         AppleEvent  theAppleEvent;
  92.         AppleEvent  replyEvent;
  93.     
  94.         err = AECreateDesc(typeProcessSerialNumber, (Ptr)&graphicsBugProcess, sizeof(graphicsBugProcess), &targetAddress);
  95.         err = AECreateAppleEvent(kCoreEventClass, kAEOpenApplication, &targetAddress, kAutoGenerateReturnID,
  96.             kAnyTransactionID, &theAppleEvent);
  97.         err = AESend(&theAppleEvent, &replyEvent, kAENoReply | kAECanInteract | kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, nil, nil);
  98.         AEDisposeDesc(&theAppleEvent);
  99.         
  100.         //  select the bottom of the window just in case it was pointing to the top
  101.         err = AECreateAppleEvent(bugEventClass, selectBugWindowPartEvent, &targetAddress, kAutoGenerateReturnID,
  102.             kAnyTransactionID, &theAppleEvent);
  103.         {   boolean selectTop = false;
  104.             
  105.             err = AEPutParamPtr(&theAppleEvent, chooseWindowPartBoolean, keyDirectObject, (Ptr)&selectTop, sizeof(selectTop));
  106.         }
  107.         err = AESend(&theAppleEvent, &replyEvent, kAENoReply | kAECanInteract | kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, nil, nil);
  108.         AEDisposeDesc(&theAppleEvent);
  109.         
  110.         // create a second event that contains the error message and paste it into the window
  111.         err = AECreateAppleEvent(bugEventClass, pasteBugTextEvent, &targetAddress, kAutoGenerateReturnID,
  112.             kAnyTransactionID, &theAppleEvent);
  113.         {   Str255 quoted;
  114.             long length = strlen(str);
  115.             unsigned char *quotePtr = quoted;
  116.             
  117.             *quotePtr++ = '"';
  118.             BlockMove(str, quotePtr, length);
  119.             quotePtr += length;
  120.             if (num)
  121.             {   register int digit;
  122.                 register int counter = 8;
  123.                 
  124.                 BlockMove(": 0", quotePtr, 3);
  125.                 quotePtr += 3;
  126.                 *quotePtr++ = 'X';
  127.                 do {
  128.                     num = num << 4 | num >> 28;
  129.                     digit = num & 0xf;
  130.                     *quotePtr++ = digit > '9' ? digit - '9' + 'A' - 1 : digit + '0';
  131.                 } while (--counter);
  132.             }
  133.             *quotePtr++ = '"';
  134.             *quotePtr++ = returnChar;
  135.             err = AEPutParamPtr(&theAppleEvent, errorString, keyDirectObject, (Ptr) quoted, quotePtr - quoted);
  136.         }
  137.         err = AESend(&theAppleEvent, &replyEvent, kAEWaitReply | kAECanInteract | kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, nil, nil);
  138.         AEDisposeDesc(&theAppleEvent);
  139.         AEDisposeDesc(&targetAddress);
  140.     }
  141. #else
  142.     {
  143.         if (GXGetGraphicsBugParametersPointer(&blockPointer))
  144.         {   char lastToggle;
  145.             long idleCount = 0;
  146.             
  147.             while (blockPointer->status <= graphicsBugRunning) {
  148.                 if (lastToggle != blockPointer->toggle) {
  149.                     lastToggle = blockPointer->toggle;
  150.                     idleCount = 0;
  151.                 } else {
  152.                     idleCount++;
  153.                     if (idleCount > maximumIdle)
  154.                         return false; /* timed out */
  155.                 }
  156.                 EventAvail(everyEvent, &theEvent);
  157.             }
  158.             {   register char *destStringPtr = blockPointer->string;
  159.                 register const char *sourceStringPtr = string;
  160.                 static char requestChars[] = "nwe";
  161.                 
  162.                 blockPointer->message = message;
  163.                 if (command >= noticeMessage && command <= errorMessage) {
  164.                     *destStringPtr++ = 'g';
  165.                     *destStringPtr++ = requestChars[command];
  166.                 } else if (sourceStringPtr) {
  167.                     if (command == debugMessage)
  168.                         *destStringPtr++ = '"';
  169.                     while (*sourceStringPtr)
  170.                         *destStringPtr++ = *sourceStringPtr++;
  171.                     if (command == debugMessage)
  172.                         *destStringPtr++ = '"';
  173.                 }
  174.                 blockPointer->command = executeCommand;
  175.             }
  176.             if (waitForCompletion) {
  177.                 while (blockPointer->status) {
  178.                     if (lastToggle != blockPointer->toggle) {
  179.                         lastToggle = blockPointer->toggle;
  180.                         idleCount = 0;
  181.                     } else {
  182.                         idleCount++;
  183.                         if (idleCount > maximumIdle)
  184.                             return false;
  185.                     }
  186.                     EventAvail(everyEvent, &theEvent);
  187.                 }
  188.             }
  189.         } else
  190.             return false;
  191.     }
  192. #endif
  193.     return true;
  194. }
  195.  
  196. static boolean SendGraphicsBugMessage(const char *str, long message, long reference)
  197. {
  198.     reference = 0;  /* so MPW will not complain */
  199.     if (SendMessageToGraphicsBug(debugMessage, str, message, true) == false) {
  200.         GXSetUserGraphicsDebug(nil, 0);
  201.         return false;
  202.     }
  203.     return true;
  204. }
  205.  
  206. void DirectDebugMessageToGraphicsBug(void)
  207. {
  208.     GXSetUserGraphicsDebug(SendGraphicsBugMessage, 0);
  209. }
  210.